#include "ShipGroup.h"
#include "Commander.h"

ShipGroup::ShipGroup( int x )
{
	ships.push_back(x);
}

ShipGroup::~ShipGroup(void)
{
}

float ShipGroup::dis( ShipGroup x )
{
	Commander& c = Commander::getInstance();
	vector<Fatworm>& ours = c.ours;

	float minDis = FLT_MAX;
	for (unsigned int i=0; i<ships.size(); ++i)
		for (unsigned int j=0; j<x.size(); ++j) {
			float dis = (ours[ships[i]].pos - ours[x.ships[j]].pos).norm();
			if (dis < minDis) minDis = dis;
		}
	return minDis;
}

float ShipGroup::dis(Fatworm x) {
	Commander& c = Commander::getInstance();
	vector<Fatworm>& ours = c.ours;

	float minDis = FLT_MAX;
	for (unsigned int i=0; i<ships.size(); ++i) {
		float dis = (ours[ships[i]].pos - x.pos).norm();
		if (dis < minDis) minDis = dis;
	}
	return minDis;
}

void ShipGroup::merge( ShipGroup x )
{
	for (unsigned int i = 0; i < x.size(); ++i)
		ships.push_back(x.ships[i]);
	x.ships.clear();
}

unsigned int ShipGroup::size( void )
{
	return ships.size();
}

bool ShipGroup::findEnemy( void )
{
	return enemyShips.size() > 0;
}

void ShipGroup::attack( void )
{
	Commander& c = Commander::getInstance();
	vector<Fatworm>& ours = c.ours;

	for (unsigned int i=0; i<ships.size(); ++i) {
		ours[ships[i]].attack(enemyShips[0], Fatworm::RUSH);
		cout << ours[ships[i]].curCommand << endl;
	}
}

void ShipGroup::updateInfo( void )
{
	Commander& c = Commander::getInstance();
	vector<Fatworm>& enemies = c.enemies;

	enemyShips.clear();
	for (unsigned int i=0; i<enemies.size(); ++i) {
		if (dis(enemies[i]) < RangeOfView)
			enemyShips.push_back(i);
	}
}

